//package com.niit.xlztest.WeSocket;
//
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import jakarta.servlet.http.HttpSession;
//import org.apache.catalina.security.SecurityConfig;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.http.server.ServletServerHttpRequest;
//import org.springframework.http.server.ServletServerHttpResponse;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.MessageChannel;
//import org.springframework.messaging.simp.config.ChannelRegistration;
//import org.springframework.messaging.simp.config.MessageBrokerRegistry;
//import org.springframework.messaging.simp.stomp.StompCommand;
//import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
//import org.springframework.messaging.support.ChannelInterceptor;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.web.socket.WebSocketHandler;
//import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
//import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
//import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
//import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
//import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
//import java.util.Map;
//
//@Configuration
//@EnableWebSocketMessageBroker
//@Import(SecurityConfig.class)
//public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
//
//    @Autowired
//    private AuthenticationSuccessHandler authenticationSuccessHandler;
//
//    @Override
//    public void registerStompEndpoints(StompEndpointRegistry registry) {
//        registry.addEndpoint("/websocket") // 配置 WebSocket 入口地址
//                .setAllowedOrigins("*") // 允许跨域访问
//                .withSockJS() // 开启 SockJS 支持
//                .setInterceptors(httpSessionHandshakeInterceptor()); // 设置在握手过程中使用的拦截器
//    }
//
//    @Override
//    public void configureMessageBroker(MessageBrokerRegistry registry) {
//        registry.enableSimpleBroker("/topic"); // 在这个地址开启简单消息代理，用于处理客户端发送的消息
//        registry.setApplicationDestinationPrefixes("/app"); // 设置客户端发送的消息前缀，用于映射消息的目的地
//    }
//
//    @Bean
//    public HttpSessionHandshakeInterceptor httpSessionHandshakeInterceptor() {
//        return new HttpSessionHandshakeInterceptor() {
//            @Override
//            public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
//                HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
//                HttpServletResponse servletResponse = ((ServletServerHttpResponse) response).getServletResponse();
//                HttpSession session = servletRequest.getSession();
//                attributes.put(HttpSession.class.getName(), session);
//                return super.beforeHandshake(request, response, wsHandler, attributes);
//            }
//        };
//    }
//
//    @Override
//    public void configureClientInboundChannel(ChannelRegistration registration) {
//        registration.interceptors(new UserInterceptor()); // 添加消息处理前的拦截器
//    }
//
//    private static class UserInterceptor implements ChannelInterceptor {
//
//        @Override
//        public Message<?> preSend(Message<?> message, MessageChannel channel) {
//            StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
//            if (StompCommand.CONNECT.equals(accessor.getCommand())) {
//                HttpSession session = accessor.getSessionAttributes().get(HttpSession.class.getName(), HttpSession.class);
//                String username = (String) session.getAttribute("username");
//                if (username != null) {
//                    Authentication authentication = new UsernamePasswordAuthenticationToken(username, null);
//                    SecurityContextHolder.getContext().setAuthentication(authentication);
//                }
//                accessor.setUser(authenticationSuccessHandler.authenticate(new UsernamePasswordAuthenticationToken(username, null))); // 将当前用户设置到消息头中
//            }
//            return message;
//        }
//    }
//
//}
